package com.smh.demo1.a17;

import com.smh.demo1.a17.annotation.Token;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * @author shiminghui
 * @date 2025/4/24 20:14
 * @description: TODO
 */
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    // 判断是否支持这个参数,即判断是否有@Token注解
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(Token.class);
    }

    @Override
    // 解析参数,并将参数注入到方法参数中
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String token = webRequest.getHeader("token");
        return token;
    }
}
